﻿using EmoneyWeiXinCommon;
using EmoneyWeiXinDAL;
using EmoneyWeiXinEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EmoneyWeiXinBLL
{
    public class Vm_Dms_GetnewsbycodeBLL
    {
        public string GetLHMsg()
        {
            string str = "";
            var lst = GetPoint_LH();
            if (lst != null && lst.Count >= 2)
            {
                str = lst[1].F013V_12001;
                if (str.Length > 60)
                {
                    str = str.Substring(0, 60) + "...";
                }
            }
            return "(1)" + str;
        }

        /// <summary>
        /// 获取今日利好提示
        /// </summary>
        /// <returns></returns>
        public List<Vm_Dms_GetnewsbycodeEntity> GetPoint_LH()
        {
            string key = "EMoney.WeiXin.BLL.Vm_Dms_GetnewsbycodeBLL.GetPoint_LH";
            Vm_Dms_GetnewsbycodeDAL dal = new Vm_Dms_GetnewsbycodeDAL();
            MemcachedObject cache = new MemcachedObject();
            List<Vm_Dms_GetnewsbycodeEntity> lst = cache.Get<List<Vm_Dms_GetnewsbycodeEntity>>(key);
            if (lst == null || lst.Count < 1)
            {
                lst = dal.GetPoint_LH();
                cache.Add(key, lst, 4, 0, 0);
            }
            return lst;
        }

        /// <summary>
        /// 获取走势
        /// </summary>
        /// <returns></returns>
        public Vm_Dms_GetnewsbycodeEntity GetZouShi()
        {
            try
            {
                string key = "EMoney.WeiXin.BLL.Vm_Dms_GetnewsbycodeBLL.GetZouShi";
                Vm_Dms_GetnewsbycodeDAL dal = new Vm_Dms_GetnewsbycodeDAL();
                Vm_Dms_GetnewsbycodeEntity model = new Vm_Dms_GetnewsbycodeEntity();
                MemcachedObject Cache = new MemcachedObject();
                model = Cache.Get<Vm_Dms_GetnewsbycodeEntity>(key);
                if (model == null)
                {
                    model = dal.GetZouShi();
                    Cache.Add(key, model, 0, 30, 0);
                }
                return model;
            }
            catch (Exception ex)
            {
                return null;
            }
        }


        public ZDFEntity GetZDF()
        {
            // string key = "EMoney.WeiXin.BLL.Vm_Dms_GetnewsbycodeBLL.GetZDF";
            List<ZDFEntity> model = new List<ZDFEntity>();

            // string zdf = Cache.Get<string>(key);
            string zdf = "";
            if (string.IsNullOrEmpty(zdf))
            {
                zdf = GetZDFs();
                if (!string.IsNullOrEmpty(zdf))
                {
                    model = JsonTools.ToObject<List<ZDFEntity>>(zdf);
                    // Cache.Add(key, model, DateTime.Now.AddHours(4));
                }
            }
            if (model.Count > 0 && model != null)
            {
                return model[0];
            }
            else
            {
                return null;
            }
        }

        public string GetZDFs()
        {
            try
            {
                string key = "EMONEY_SDD_2QUOTES_sh000001";
                string str = "";

                byte[] btGet = MemcachedTools.Get(key) as byte[];
                if (btGet != null)
                    str = System.Text.Encoding.GetEncoding("GB2312").GetString(btGet);
                if (string.IsNullOrEmpty(str))
                    return "";
                return str;
            }
            catch (Exception)
            {
                return "111";
            }
        }
    }
}
